<template>
  <el-dialog :title="title" :visible.sync="visible" width="900px" append-to-body>
    <el-form ref="form" inline :model="form" :rules="rules" label-width="170px">
      <el-form-item label="activeInd名称" prop="activeInd">
        <el-input
          v-model="form.activeInd"
          placeholder="请输入activeInd名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="amount名称" prop="amount">
        <el-input
          v-model="form.amount"
          placeholder="请输入amount名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="backTime名称" prop="backTime">
        <el-date-picker
          style="width: 240px"
          v-model="form.backTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="backWorkerId名称" prop="backWorkerId">
        <el-input
          v-model="form.backWorkerId"
          placeholder="请输入backWorkerId名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="checkIndex名称" prop="checkIndex">
        <el-input
          v-model="form.checkIndex"
          placeholder="请输入checkIndex名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="ciden名称" prop="ciden">
        <el-select
          v-model="form.ciden"
          placeholder="ciden名称"
          clearable
          style="width: 240px"
        >
          <el-option :value="0" label="隐藏"></el-option>
          <el-option :value="1" label="显示"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="contractContent名称" prop="contractContent">
        <editor v-model="form.contractContent" :min-height="192"/>
      </el-form-item>
      <el-form-item label="合同编号" prop="contractId">
        <el-input
          v-model="form.contractId"
          placeholder="请输入合同编号"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="contractType名称" prop="contractType">
        <el-input
          v-model="form.contractType"
          placeholder="请输入contractType名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="dispatchWorkerId名称" prop="dispatchWorkerId">
        <el-input
          v-model="form.dispatchWorkerId"
          placeholder="请输入dispatchWorkerId名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="getTime名称" prop="getTime">
        <el-date-picker
          style="width: 240px"
          v-model="form.getTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="isNew名称" prop="isNew">
        <el-input
          v-model="form.isNew"
          placeholder="请输入isNew名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="printNum名称" prop="printNum">
        <el-input
          v-model="form.printNum"
          placeholder="请输入printNum名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="printTime名称" prop="printTime">
        <el-date-picker
          style="width: 240px"
          v-model="form.printTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="二维码链接" prop="qrCodeUrl">
        <el-input
          v-model="form.qrCodeUrl"
          placeholder="请输入二维码链接"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="销售订单编号" prop="saleOrderId">
        <el-input
          v-model="form.saleOrderId"
          placeholder="请输入销售订单编号"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="signingUrl名称" prop="signingUrl">
        <el-input
          v-model="form.signingUrl"
          placeholder="请输入signingUrl名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="workerId名称" prop="workerId">
        <el-input
          v-model="form.workerId"
          placeholder="请输入workerId名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="永昌合同编号" prop="ycContract">
        <el-input
          v-model="form.ycContract"
          placeholder="请输入永昌合同编号"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="ycState名称" prop="ycState">
        <el-select
          v-model="form.ycState"
          placeholder="ycState名称"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in options.ycState"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {add, getById, updateById} from "@/api/salepurchase/contract";

export default {
  name: "create",
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      visible: false,
      // 表单参数
      form: {
        activeInd: "",
        amount: "",
        backTime: "",
        backWorkerId: "",
        checkIndex: "",
        ciden: "",
        contractContent: "",
        contractId: "",
        contractType: "",
        dispatchWorkerId: "",
        getTime: "",
        isNew: "",
        printNum: "",
        printTime: "",
        qrCodeUrl: "",
        saleOrderId: "",
        signingUrl: "",
        workerId: "",
        ycContract: "",
        ycState: "",
      },
      // 表单校验
      rules: {
        activeInd: [
          {required: true, message: "activeInd名称不能为空", trigger: "blur"}
        ],
        amount: [
          {required: true, message: "amount名称不能为空", trigger: "blur"}
        ],
        backTime: [
          {required: true, message: "backTime名称不能为空", trigger: "change"}
        ],
        backWorkerId: [
          {required: true, message: "backWorkerId名称不能为空", trigger: "blur"}
        ],
        checkIndex: [
          {required: true, message: "checkIndex名称不能为空", trigger: "blur"}
        ],
        ciden: [
          {required: true, message: "ciden名称不能为空", trigger: "change"}
        ],
        contractContent: [
          {required: true, message: "contractContent名称不能为空", trigger: "blur"}
        ],
        contractId: [
          {required: true, message: "合同编号不能为空", trigger: "blur"}
        ],
        contractType: [
          {required: true, message: "contractType名称不能为空", trigger: "blur"}
        ],
        dispatchWorkerId: [
          {required: true, message: "dispatchWorkerId名称不能为空", trigger: "blur"}
        ],
        getTime: [
          {required: true, message: "getTime名称不能为空", trigger: "change"}
        ],
        isNew: [
          {required: true, message: "isNew名称不能为空", trigger: "blur"}
        ],
        printNum: [
          {required: true, message: "printNum名称不能为空", trigger: "blur"}
        ],
        printTime: [
          {required: true, message: "printTime名称不能为空", trigger: "change"}
        ],
        qrCodeUrl: [
          {required: true, message: "二维码链接不能为空", trigger: "blur"}
        ],
        saleOrderId: [
          {required: true, message: "销售订单编号不能为空", trigger: "blur"}
        ],
        signingUrl: [
          {required: true, message: "signingUrl名称不能为空", trigger: "blur"}
        ],
        workerId: [
          {required: true, message: "workerId名称不能为空", trigger: "blur"}
        ],
        ycContract: [
          {required: true, message: "永昌合同编号不能为空", trigger: "blur"}
        ],
        ycState: [
          {required: true, message: "ycState名称不能为空", trigger: "change"}
        ]
      },
      options: {
        ycState: [
          {value: 0, label: '未提交'},
          {value: 1, label: '待审核'},
          {value: 2, label: '已审核'},
          {value: -1, label: '不通过'},
        ]
      },
    }
  },
  methods: {
    open(id) {
      if (id) {
        getById(id).then(response => {
          this.visible = true
          this.$nextTick(() => {
            this.form = response.data
          })
          this.title = "修改合同"
        })
      } else {
        this.visible = true
        this.$nextTick(() => {
          this.reset()
        })
        this.title = "新增合同"
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateById(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.$emit('confirm')
            })
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.$emit('confirm')
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.visible = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.$refs.form.resetFields()
    },
  }
}
</script>
